//
//  ByUISetPSWVC.m
//  oc_knowledge
//
//  Created by justiOS on 17/1/19.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUISetPSWVC.h"

// view
#import "ByViewRegisterStep.h"
// net
#import "ByApiUser.h"

@interface ByUISetPSWVC ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *pswTF1;
@property (nonatomic, strong) UITextField *pswTF2;
@property (nonatomic, strong) UIButton *deleButton1;
@property (nonatomic, strong) UIButton *deleButton2;
@property (nonatomic, strong) UIButton *registerButton;
@end

@implementation ByUISetPSWVC

#pragma mark getter
- (UITextField *)pswTF1 {
    if (!_pswTF1) {
        _pswTF1 = [[UITextField alloc] init];
        _pswTF1.secureTextEntry = YES;
        _pswTF1.placeholder = @"请输入密码";
        _pswTF1.font = FONT(14);
        _pswTF1.tintColor = BY_THEME_COLOR;
        _pswTF1.delegate = self;
        [_pswTF1 addTarget:self action:@selector(mobleTFChange:) forControlEvents:UIControlEventEditingChanged];
        [_pswTF1 becomeFirstResponder];
    }
    return _pswTF1;
}


- (UITextField *)pswTF2 {
    if (!_pswTF2) {
        _pswTF2 = [[UITextField alloc] init];
        _pswTF2.secureTextEntry = YES;
        _pswTF2.placeholder = @"请再次输入密码";
        _pswTF2.font = FONT(14);
        _pswTF2.tintColor = BY_THEME_COLOR;
        _pswTF2.delegate = self;
        [_pswTF2 addTarget:self action:@selector(mobleTFChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _pswTF2;
}


- (UIButton *)deleButton1 {
    if (!_deleButton1) {
        _deleButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleButton1 setImage:[UIImage imageNamed:@"Register_delete"] forState:UIControlStateNormal];
        [_deleButton1 addTarget:self action:@selector(deleAction1:) forControlEvents:UIControlEventTouchUpInside];
        _deleButton1.hidden = YES;
    }
    return _deleButton1;
}

- (UIButton *)deleButton2 {
    if (!_deleButton2) {
        _deleButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleButton2 setImage:[UIImage imageNamed:@"Register_delete"] forState:UIControlStateNormal];
        [_deleButton2 addTarget:self action:@selector(deleAction2:) forControlEvents:UIControlEventTouchUpInside];
        _deleButton2.hidden = YES;
    }
    return _deleButton2;
}


- (UIButton *)registerButton {
    if (!_registerButton) {
        _registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _registerButton.layer.cornerRadius = 3;
        _registerButton.backgroundColor = BY_THEME_COLOR;
        [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
        [_registerButton addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerButton;
}

#pragma mark  UI
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置密码";
    [self addLeftBackBarButtonItem];
    [self createViews];
    self.view.backgroundColor = TABLE_COLOR;
    
    self.view.userInteractionEnabled = true;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapEmptyAction:)];
    [self.view addGestureRecognizer:tap];
    
}


- (void)createViews {
    ByViewRegisterStep *view = [[ByViewRegisterStep alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    view.step = RegisterStepThree;
    [self.view addSubview:view];
    
    
    UIView *TFBG = [[UIView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, 80)];
    TFBG.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:TFBG];
    
    self.pswTF1.frame = CGRectMake(10, 10, SCREEN_WIDTH - 40, 20);
    self.pswTF2.frame = CGRectMake(10, 50, SCREEN_WIDTH - 40, 20);
    self.deleButton1.frame = CGRectMake(SCREEN_WIDTH - 30, 10, 20, 20);
    self.deleButton2.frame = CGRectMake(SCREEN_WIDTH - 30, 50, 20, 20);
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 40, SCREEN_WIDTH - 10, 1)];
    line.backgroundColor = BY_LINE_COLOR;
    [TFBG addSubview:line];
    
    [TFBG addSubview:self.pswTF1];
    [TFBG addSubview:self.pswTF2];
    [TFBG addSubview:self.deleButton1];
    [TFBG addSubview:self.deleButton2];

    
    self.registerButton.frame = CGRectMake(10, TFBG.bottom + 10, SCREEN_WIDTH - 20, 40);
    [self.view addSubview:self.registerButton];
}

#pragma mark 点击事件
- (void)deleAction1:(UIButton *)button {
    self.pswTF1.text = nil;
    button.hidden = YES;
}

- (void)deleAction2:(UIButton *)button {
    self.pswTF2.text = nil;
    button.hidden = YES;
}

- (void)registerAction:(UIButton *)button {
    
    if (![_pswTF1.text isEqualToString:_pswTF2.text]) {
        [ByCoreToolAlert showOnlyText:@"两次输入的密码不一致，请重新输入"];
        return;
    }
    
    BOOL isok = [self password:_pswTF1.text];
    if (isok == false) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册失败" message:@"经过检测，密码容易被盗，建议使用字母大小写加数字乱序组合"preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:^{
            
        }];

        return;
    }
    
    
    // 注册设置密码
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.password = self.pswTF1.text;
    model.username = _phoneNumber;
    model.code = self.code;
    model.from = @"0";
    [ByCoreToolAlert showLoadind:@"设置中..." hasMasker:YES];
    [ByApiUser reigster:model success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showSuccessWithText:result.msg];
        NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:self.pswTF1.text,@"psw",_phoneNumber,@"phone", nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"registermodel" object:nil userInfo:dic];
        [self by_normalPopVC:@"ByUINRegisterVC"];
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}


- (void)tapEmptyAction:(UITapGestureRecognizer *)tap {
    
    [UIView animateWithDuration:0.4 animations:^{
        [self.pswTF1 resignFirstResponder];
        [self.pswTF2 resignFirstResponder];
    }];
}


#pragma mark tf delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (textField == self.pswTF1) {
        if (textField.text.length > 0) {
            self.deleButton1.hidden = NO;
        }else{
            self.deleButton1.hidden = YES;
        }
    }else{
        if (textField.text.length > 0) {
            self.deleButton2.hidden = NO;
        }else{
            self.deleButton2.hidden = YES;
        }
    }

}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.pswTF1) {
        self.deleButton1.hidden = YES;
    }else{
        self.deleButton2.hidden = YES;
    }
}

- (void)mobleTFChange:(UITextField *)textField {
    if (textField == self.pswTF1) {
        if (textField.text.length > 0) {
            self.deleButton1.hidden = NO;
        }else{
            self.deleButton1.hidden = YES;
        }
    }else {
        if (textField.text.length > 0) {
            self.deleButton2.hidden = NO;
        }else{
            self.deleButton2.hidden = YES;
        }
    }
}



// 判断密码的强度

- (BOOL)password:(NSString *)password
{
    
    NSString *pattern = @"[a-zA-Z0-9]{6,19}+$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    
    BOOL isMatch = [pred evaluateWithObject:password];
    
    if (isMatch) {
        
        int count = 0;
        NSString *newPassword = [password substringToIndex:1];
        for (int i = 0; i < password.length; i++) {
            NSString *newPassword1;
            if (i == 0) {
                newPassword1 = [password substringToIndex:i + 1]; //当 i 为 0 的时候  取下表为 1 的字符串
            }else{
                //当 i 大于 0 时 我们取下标为 i + 1 新的string.length 为 i 个 所有我们再从后面往前面取
                newPassword1 = [[password substringToIndex:i + 1] substringFromIndex:i];
            }
            if ([newPassword1 isEqualToString:newPassword]) {
                count++;
            }
        }
        
        if (count == password.length) {
            return NO; // 这里说明 count个相同的字符串，也就是所有密码输入一样了
        }
        
        if ([password isEqualToString:@"123456"] || [password isEqualToString:@"qwerty"] || [password isEqualToString:@"654321"]){
            return NO;
        }
        
        
    }
    
    return isMatch;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
